package com.iris.live.services.services.impl;


import com.iris.live.services.data.models.UpdateInfoModel;
import com.iris.live.services.data.repositories.PCUpdateRepository;
import com.iris.live.services.data.repositories.PCUpdateSqlRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import com.iris.live.services.services.PCUpdateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SallyGo on 2016/12/10.
 */
@Service
public class PCUpdateServiceImpl implements PCUpdateService {

    private static Logger logger = LoggerFactory.getLogger(PCUpdateServiceImpl.class);

    @Autowired
    private PCUpdateRepository pcUpdateRepository;

    @Autowired
    private PCUpdateSqlRepository pcUpdateSqlRepository;

    @Override
    @Transactional
    public ResultData getLocalVer(GetLocalVer request, String dlcode, String user) {
        ResultData resultData = new ResultData();
        PageData pageData = new PageData();
        List<UpdateInfoModel> list = new ArrayList<>();
        try {
            //寻找是否有单店版本
            list = pcUpdateRepository.getNowVerByCode(dlcode, request.state + "");
            if (list == null || list.size() == 0) {
                //寻找全部版本
                list = pcUpdateRepository.getNowVerByNull(request.state + "");
                if (list == null || list.size() == 0) {
                    //没有数据
                    resultData.setResult("true");
                    resultData.setMessage("成功");
                } else {
                    //有数据
                    resultData.setResult("true");
                    List newList = new ArrayList<>();
                    newList.add(list.get(0));
                    list = newList;
                    Integer nowVer = Integer.valueOf(list.get(0).getVer());
                    //判断是否更新
                    if (request.ver != nowVer) {
                        resultData.setMessage("更新");
                    } else {
                        resultData.setMessage("成功");
                    }
                }
            } else {
                //有数据
                resultData.setResult("true");
                List newList = new ArrayList<>();
                newList.add(list.get(0));
                list = newList;
                Integer nowVer = Integer.valueOf(list.get(0).getVer());
                //判断是否更新
                if (request.ver != nowVer) {
                    resultData.setMessage("更新");
                } else {
                    resultData.setMessage("成功");
                }
            }
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("查询失败");
            logger.error("查询失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        pageData.setData(list);
        pageData.setTotal(list.size());
        resultData.setPageData(pageData);
        return resultData;
    }
}
